{% extends 'base.html' %}
{% load static %}

{% block title %}用户详情 - {{ target_user.username }} - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>用户详情</h1>
        <div>
            <a href="{% url 'users:user_list' %}" class="btn btn-outline-secondary me-2">
                <i class="bi bi-arrow-left"></i> 返回列表
            </a>
            <a href="{% url 'users:user_edit' target_user.pk %}" class="btn btn-outline-primary me-2">
                <i class="bi bi-pencil"></i> 编辑
            </a>
            {% if target_user != request.user %}
            <form method="post" action="{% url 'users:user_toggle_active' target_user.pk %}" class="d-inline">
                {% csrf_token %}
                <button type="submit" class="btn btn-{% if target_user.is_active %}warning{% else %}success{% endif %}">
                    <i class="bi bi-{% if target_user.is_active %}x-circle{% else %}check-circle{% endif %}"></i>
                    {% if target_user.is_active %}禁用{% else %}启用{% endif %}
                </button>
            </form>
            {% endif %}
        </div>
    </div>

    {% if messages %}
    <div class="alert alert-info">
        {% for message in messages %}
        <div>{{ message }}</div>
        {% endfor %}
    </div>
    {% endif %}

    <div class="row">
        <!-- 基本信息 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">基本信息</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-6 mb-3">
                            <strong>用户名:</strong><br>
                            {{ target_user.username }}
                            {% if target_user.is_staff %}
                            <span class="badge bg-danger ms-1">管理员</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>用户状态:</strong><br>
                            {% if target_user.is_active %}
                            <span class="badge bg-success">启用</span>
                            {% else %}
                            <span class="badge bg-danger">禁用</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>姓名:</strong><br>
                            {% if target_user.first_name or target_user.last_name %}
                            {{ target_user.first_name }} {{ target_user.last_name }}
                            {% else %}
                            <span class="text-muted">未设置</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>邮箱:</strong><br>
                            {{ target_user.email|default:"未设置" }}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>注册时间:</strong><br>
                            {{ target_user.date_joined|date:"Y-m-d H:i" }}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>最后登录:</strong><br>
                            {% if target_user.last_login %}
                            {{ target_user.last_login|date:"Y-m-d H:i" }}
                            {% else %}
                            <span class="text-muted">从未登录</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 角色和配置信息 -->
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="card-title mb-0">角色配置</h5>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-6 mb-3">
                            <strong>用户角色:</strong><br>
                            {% if target_user.profile %}
                            <span class="badge bg-{% if target_user.profile.role == 'admin' %}danger{% elif target_user.profile.role == 'technician' %}info{% else %}secondary{% endif %}">
                                {{ target_user.profile.get_role_display }}
                            </span>
                            {% else %}
                            <span class="badge bg-warning">未配置</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>所属部门:</strong><br>
                            {% if target_user.profile and target_user.profile.department %}
                            {{ target_user.profile.department }}
                            {% else %}
                            <span class="text-muted">未设置</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>联系电话:</strong><br>
                            {% if target_user.profile and target_user.profile.phone %}
                            {{ target_user.profile.phone }}
                            {% else %}
                            <span class="text-muted">未设置</span>
                            {% endif %}
                        </div>
                        <div class="col-6 mb-3">
                            <strong>员工编号:</strong><br>
                            {% if target_user.profile and target_user.profile.employee_id %}
                            {{ target_user.profile.employee_id }}
                            {% else %}
                            <span class="text-muted">未设置</span>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 用户活动统计 -->
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">用户活动统计</h5>
                </div>
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-md-3 mb-3">
                            <div class="border rounded p-3">
                                <h4 class="text-primary">{{ created_tickets }}</h4>
                                <small class="text-muted">创建的工单</small>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="border rounded p-3">
                                <h4 class="text-info">{{ assigned_tickets }}</h4>
                                <small class="text-muted">处理的工单</small>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="border rounded p-3">
                                <h4 class="text-success">{{ owned_assets }}</h4>
                                <small class="text-muted">拥有的设备</small>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="border rounded p-3">
                                <h4 class="text-warning">{{ maintenance_records }}</h4>
                                <small class="text-muted">维修记录</small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
